package com.example.lenovo.findmyclass.Buildings;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import com.example.lenovo.findmyclass.MainActivity;
import com.example.lenovo.findmyclass.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

public class BuildingFour extends Activity {
    private Button back;
    Calendar cal;
    private TextView time;
    private String year,month,week,day;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_building_four);
        back = findViewById(R.id.back);
        time=findViewById(R.id.time);
        sendRequestWithOkHttp();

        back=findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(BuildingFour.this, MainActivity.class);
                startActivity(intent);
            }
        });
        cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));

        year = String.valueOf(cal.get(Calendar.YEAR));
        month = String.valueOf(cal.get(Calendar.MONTH)+1);
        week = String.valueOf(cal.get(Calendar.DAY_OF_WEEK));
        if("1".equals(week)){
            week ="天";
        }else if("2".equals(week)){
            week ="一";
        }else if("3".equals(week)){
            week ="二";
        }else if("4".equals(week)){
            week ="三";
        }else if("5".equals(week)){
            week ="四";
        }else if("6".equals(week)){
            week ="五";
        }else if("7".equals(week)){
            week ="六";
        }
        day = String.valueOf(cal.get(Calendar.DATE));
        time.setText(year+"年"+month+"月"+day+"日"+"周"+week);
    }



        private void sendRequestWithOkHttp(){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        OkHttpClient client = new OkHttpClient();
                        Request request = new Request.Builder().url("http://10.217.8.195/f_data.json").build();
                        Response response = client.newCall(request).execute();
                        String responseData = response.body().string();
                        parseJSONWithJSONObject(responseData);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
        //使用JSONObject解析JSON数据
        private  void parseJSONWithJSONObject(String jsonData){
            try {
                JSONArray jsonArray = new JSONArray(jsonData);
                Resources res = getResources();
                for (int j = 1; j < 7;j++){
                    for (int i = 0;i < 12; i++){
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String value = jsonObject.getString("value");

                        int id = res.getIdentifier("a" + j + "-" + (i+1), "id", getPackageName());
                        TextView textView = (TextView) findViewById(id);
                        textView.setText(value);
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

}


